package cn.com.miaoto.business.timer;

import cn.com.miaoto.base.utils.MailUtils;
import cn.com.miaoto.business.service.VisitService;
import cn.com.miaoto.business.vo.VisitInfo;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by guo.chen on 2018/9/26.
 */
@EnableScheduling
@Component
public class SendVisitEmail {

    @Resource
    private VisitService visitService;
    @Resource
    private MailUtils mailUtils;

//    @Scheduled(cron="0 0/1 * * * ?")
//    public void test(){
//        System.out.println("测试定时任务开始=="+new Date());
//    }

    @Scheduled(cron="0 30 17 * * ?")
    public void sendVisitEmail(){
        System.out.println("每天循环的定时任务开始=="+new Date());
        Map params = new HashMap();
        LocalDateTime now = LocalDateTime .now();
        params.put("startTime",now);
        params.put("endTime",now.plusDays(1));
        List<VisitInfo> visitInfoList = visitService.selectVisitForTime(params);
        for(VisitInfo visitInfo : visitInfoList){
            String deliver = "crystal@miaoto.com.cn";
            String receiver = visitInfo.getEmail();
            String[] carbonCopy = null;
            String subject = "渺图销售拜访提醒";
            String content = "记得明天"+visitInfo.getRemindTime()+" 到："+visitInfo.getAddress()+"拜访:"+visitInfo.getCustomName();

            try {
                mailUtils.sendSimpleEmail(deliver, receiver, carbonCopy, subject, content);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        String deliver = "1362640409@qq.com";
        String receiver = "guochen@miaoto.com.cn";
//            String receiver = visitInfo.getEmail();
        String[] carbonCopy = null;
        String subject = "This is a test email";
        String content = "This is a test content";
        new MailUtils().sendSimpleEmail(deliver, receiver, carbonCopy, subject, content);
    }
}
